package com.dp.system.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

@Service
public class MailService {
    private static final Logger logger = LoggerFactory.getLogger(MailService.class);
    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.recipient}")
    private String recipient;

    public void sendAlarmEmail(String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("1138734196@qq.com"); // 显式设置发件人（与 username 一致）
        message.setTo(recipient);
        message.setSubject(subject);
        message.setText(content);

        try {
            mailSender.send(message);
            logger.info("报警邮件发送成功");
        } catch (Exception e) {
            logger.error("邮件发送失败：{}", e.getMessage(), e);
            // 可添加重试逻辑或通知管理员
        }
    }
}